<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>类型化数组</title>
</head>
<body>
  <script>
    /**
     * 类型                      字节
     * Int8Array                  1
     * Unit8Array                 1
     * Unit8ClampedArray          1
     * Int16Array                 2
     * Unit16Array
     * Int32Array                 4
     * Unit32Array
     * Float32Array
     * Float64Array               8
     * BigInt64Array
     * BigUnit64Array
     */

     /**
      * ArrayBuffer 是一种数据类型，通用、固定长度的二进制缓冲区
      * DataView 底层接口，提供可以操作缓冲区中任意数据的访问器
      * */

      /** 创建缓冲区 */
      const buffer = new ArrayBuffer(16)

      /** 创建视图 */
      const int32View = new Int32Array(buffer)

      /** 操作缓冲区(4 个 4 字节的元素) */
      for(let i = 0; i < int32View.length; i ++) {
        int32View[i] = i * 2
      }
      console.log(buffer, int32View)

      /** 同一数据的多个视图 */
      const int16View = new Int16Array(buffer)
      for(let i = 0; i < int16View.length; i ++) {
        console.log(`element ${i}: ${int16View[i]} \n`)
      }

      int16View[0] = 32 // 0000 0000
      int16View[1] = 16 // 0000 0000
      console.log('16 - 32', int32View[0], int32View[1])
  </script>
</body>
</html>